<template>
  <div class="box">
    <header class="header">
      <van-nav-bar
        title="确认订单"
        @click-left="back"
        left-arrow
      ></van-nav-bar>
      <van-count-down :time="time" />
    </header>
    <div class="content">
      <van-notice-bar
        color="black"
        text="您正在安全购物环境中，请放心购买"
      ></van-notice-bar>
      <van-icon class="anquan" name="after-sale" size="24"/>
      <van-sticky>
        <div v-if="userinfo.flag === 1" class="orderaddress" @click="toAddresslist">
          <h1>{{ userinfo.name }}<i>{{ userinfo.tel }}</i></h1>
          <p class="city">{{ userinfo.province + userinfo.city + userinfo.county + userinfo.addressDetail }}</p>
          <van-icon class="right" name="arrow" />
          <p class="inf">1个包裹，预计{{ new Date().getMonth() + 1 }}月{{ new Date().getDate() + 5 }}日送达</p>
        </div>
        <div v-else class="orderaddress" @click="toAddresslist">
          <p>点击添加地址</p>
        </div>
      </van-sticky>
      <van-card
        v-for="item of orderlist"
        :key="item.proid"
        :num="item.num"
        :price="item.price"
        :title="item.proname"
        :thumb="item.proimg"
      />
      <!-- 优惠券单元格 -->
      <van-coupon-cell
        :coupons="coupons"
        :chosen-coupon="chosenCoupon"
        @click="showList = true"
      />
      <!-- 优惠券列表 -->
      <van-popup
        v-model="showList"
        round
        position="bottom"
        style="height: 90%; padding-top: 4px;"
      >
        <van-coupon-list
          :coupons="coupons"
          :chosen-coupon="chosenCoupon"
          :disabled-coupons="disabledCoupons"
          @change="onChange"
          @exchange="onExchange"
        />
      </van-popup>
      <div class="confimInfo">
        <div class="ordermoney">
          订单金额<span>￥{{ totalprice }}</span>
        </div>
        <div class="summoney">
          商品总金额<span>￥{{ totalprice }}</span>
        </div>
        <div class="kong"></div>
      </div>
      <div class="pay">
        <p v-if="chosenCoupon==0">还需支付：<span>￥{{ sumprice }}</span></p>
        <p v-else>还需支付：<span>￥{{ totalprice }}</span></p>
        <div class="hdfk">货到付款</div>
        <div class="zxzf">在线支付</div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Dialog, Sticky, Card, CouponCell, CouponList, Popup, CountDown, NoticeBar, Icon } from 'vant'
import { getOrderData } from '@/api'
Vue.use(NavBar)
Vue.use(Sticky)
Vue.use(Card)
Vue.use(CouponCell)
Vue.use(CouponList)
Vue.use(Popup)
Vue.use(CountDown)
Vue.use(NoticeBar)
Vue.use(Icon)
const coupon = {
  available: 1,
  condition: '无使用门槛',
  reason: '',
  value: 2000,
  name: '优惠券名称',
  startAt: 1489104000,
  endAt: 1514592000,
  valueDesc: '20',
  unitDesc: '元'
}
export default {
  data () {
    return {
      userinfo: {},
      orderlist: [],
      status: 0,
      orderid: '',
      totalprice: 0,
      valueDesc: 20,
      chosenCoupon: -1,
      coupons: [coupon],
      disabledCoupons: [coupon],
      showList: false,
      time: 20 * 60 * 1000
    }
  },
  computed: {
    sumprice: function () {
      return this.totalprice - this.valueDesc
    }
  },
  mounted () {
    const { orderid } = this.$route.params
    getOrderData({ orderid }).then(res => {
      console.log(res.data)
      this.userinfo = res.data.data.userinfo
      this.orderlist = res.data.data.orderlist
      this.status = res.data.data.status
      this.orderid = orderid
      this.totalprice = res.data.data.totalprice
    })
  },
  methods: {
    back () {
      Dialog.confirm({
        message: '便宜不等人，请三思而行'
      }).then(() => {
        // on confirm
        // 离开时删除该订单（作业）
        this.$router.go(-1)
      }).catch(() => {
        // on cancel
      })
    },
    toAddresslist () {
      this.$router.push('/addresslist?orderid=' + this.orderid)
    },
    onChange (index) {
      this.showList = false
      this.chosenCoupon = index
    },
    onExchange (code) {
      this.coupons.push(coupon)
    }
  }
}
</script>

<style lang="scss">
body {
  background-color: #f3f4f5;
}
.van-nav-bar {
  color:#575757;
}
.van-notice-bar {
  text-indent: 20px;
  background-color: #e8eaf4;
}
.anquan {
  color: green;
  position: relative;
  left: 10px;
  top: -32px;
}
.van-count-down {
  line-height: 0.46rem;
  color: #e80080;
  position: absolute;
  top: 0;
  right: 95px;
  z-index: 5;
}
.orderaddress {
  padding: 16px;
  min-height: 50px;
  margin: 0 0 20px 0;
  color: white;
  background-color: #656c89;
  position: relative;
  h1 {
    font-size: 18px;
    font-weight: normal;
    i {
      padding-left: 16px;
      font-size: 14px;
    }
  }
  .city {
    margin-bottom: 16px;
  }
  .inf {
    padding-top: 16px;
    border-top: 2px solid #585e76;
  }
  .right {
    font-size: 20px;
    position: absolute;
    top: 32px;
    right: 15px;
    color: white;
  }
}
.van-coupon-cell {
  margin-top: 12px;
}
.popup {
  color: red;
}
.confimInfo {
  margin-top: 12px;
  background-color: white;
  .ordermoney {
    margin: 0 auto;
    width: 96%;
    height: 40px;
    line-height: 40px;
    font-weight: bold;
    border-bottom: 1px solid #dddddd;
    span {
      float: right;
    }
  }
  .summoney {
    margin: 0 auto;
    width: 96%;
    height: 40px;
    line-height: 40px;
    font-weight: bold;
    span {
      float: right;
    }
  }
  .kong {
    height: 180px;
    background-color: #f3f4f5;
  }
}
.pay {
  width: 100%;
  height: 114px;
  background-color: white;
  border-top: 1px solid #dcdcdc;
  position: fixed;
  left: 0;
  bottom: 0;
  p {
    line-height: 46px;
    text-align: center;
    span {
      color: #e80080;
    }
  }
  .hdfk {
    width: 165px;
    height: 48px;
    border: 1px solid #e4e4e7;
    box-sizing: border-box;
    border-radius: 3px;
    position: absolute;
    left: 16px;
    top: 50px;
    font-size: 16px;
    line-height: 48px;
    text-align: center;
  }
  .zxzf {
    width: 165px;
    height: 48px;
    background-color: #e80080;
    border-radius: 3px;
    position: absolute;
    left: 195px;
    top: 50px;
    font-size: 16px;
    line-height: 48px;
    text-align: center;
    color: white;
  }
}
</style>
